package ua.kpi.ipsa.expert.api.net;

import java.rmi.*;
import java.util.*;

import ua.kpi.ipsa.expert.api.adminpanel.*;
import ua.kpi.ipsa.expert.api.db.*;
import ua.kpi.ipsa.expert.api.server.*;

/**
 * 
 * @author doctorpro
 * 
 */
public class DefaultAdminsConsole implements AdminsConsole {

	//
	private UserConsoleManager consoleManager;

	//
	private RemoteUserListener remoteUserListener;

	//
	private UserAdapter userAdapter = new UserAdapter();

	//
	private LifecycleManager lifeManager;

	//
	private DAO dao;

	/**
	 * 
	 * @param consoleManager
	 */
	public DefaultAdminsConsole(UserConsoleManager consoleManager,
			LifecycleManager lifeManager, DAO dao) {
		this.consoleManager = consoleManager;
		this.lifeManager = lifeManager;
		this.dao = dao;
	}

	/**
	 * 
	 */
	@Override
	public void dropUser(String userLogin) throws RemoteException,
			ConsoleException {
		consoleManager.releaseUser(userLogin);
	}

	/**
	 * 
	 */
	@Override
	public void setRemoteUserListener(RemoteUserListener remoteUserListener)
			throws RemoteException {
		this.remoteUserListener = remoteUserListener;
	}

	/**
	 * 
	 * @return
	 */
	public UserListener getUserListener() {
		return userAdapter;
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class UserAdapter implements UserListener {

		@Override
		public void userLoggedIn(String userLogin) {
			if (remoteUserListener != null) {
				try {
					remoteUserListener.userLoggedIn(userLogin);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}

		@Override
		public void userLoggedOut(String userLogin) {
			if (remoteUserListener != null) {
				try {
					remoteUserListener.userLoggedOut(userLogin);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}

		@Override
		public void userAction(String login, String methodName, String marker,
				Map<String, String> userArguments) {
			if (remoteUserListener != null) {
				try {
					remoteUserListener.userAction(login, methodName, marker,
							userArguments);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void addUser(String userLogin, String userPassword)
			throws RemoteException, ConsoleException {
		try {
			System.out.println("server: user adding: " + userLogin + "/" + userPassword);
			dao.setUser(userLogin, userPassword);
			remoteUserListener.userAdded(userLogin);
			System.out.println("server: user added: " + userLogin + "/" + userPassword);
		} catch (DataAccessException ex) {
			ex.printStackTrace(); // 1
			throw new RemoteException("DataAccessException in DAO", ex);
		}
	}

	@Override
	public void removeUser(String userLogin) throws RemoteException,
			ConsoleException {
		try {
			dao.removeUser(userLogin);
			remoteUserListener.userRemoved(userLogin);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RemoteException("DataAccessException in DAO", e);
		}
		
	}

	@Override
	public void changePassword(String userLogin, String oldPassword,
			String newPassword) throws RemoteException, ConsoleException {
		// TODO Auto-generated method stub
	}

	@Override
	public void shutdownServer() throws RemoteException {
		lifeManager.shutdown();
	}

	/**
	 * 
	 */
	@Override
	public boolean assertOnline() {
		return true;
	}

	@Override
	public boolean userExists(String login) throws RemoteException {
		try {
			return dao.getUserPassword(login) != null;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RemoteException("DataAccessException in DAO", e);
		}
	}

	@Override
	public List<String> getUsers() throws RemoteException {
		try {
			return dao.getUsers();
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RemoteException("DataAccessException", e);
		}
	}

	@Override
	public boolean isUserLogged(String userLogin) throws RemoteException {
		return false;
	}
}
